<template>
  <div class="login-page-container">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-position="left" label-width="0px" class="demo-ruleForm login-container">
      <el-form-item prop="account">
        <el-input type="text" v-model="ruleForm.account" auto-complete="off" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" auto-complete="off" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click="handleSubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import axios from 'axios'
  import server from '../../services/index'
  export default {
    data() {
      return {
        ruleForm: {
          account: '',
          checkPass: '',
        },
        rules: {
          account: [{
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }],
          checkPass: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }],
        },
      };
    },
    methods: {
      handleSubmit() {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            let loginParams = {
              userId: this.ruleForm.account,
              password: this.ruleForm.checkPass
            }
            $.ajax({
              url:server.login.url,
              type:'post',
              data:loginParams,
              dataType:'json',
              success:res => {
                if(res.code == 1){
                  this.$message({
                    message: '登录成功！',
                    type: 'success'
                  });
                  window.sessionStorage.setItem("userName",res.result.data.username);
                  window.sessionStorage.setItem("roleId",res.result.data.post);
                  window.sessionStorage.setItem("token",res.result.token);
                  this.$router.push({path: '/operationAccount'});
                }else{
                  this.$message.error(res.msg);
                }
              },
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      },
    },
    mounted(){
      window.sessionStorage.clear();
      addEventListener('keydown', (e)=> {
        if (e.keyCode === 13) {
          this.handleSubmit()
        }
      })
    }
  }
</script>

<style scoped>
  .codeImg {
    display: inline-block;
    height: 38px;
    width: 28%;
    vertical-align: bottom;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
  }

  .codeImg img {
    width: 100%;
  }

  .login-container {
    position: relative;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  label.el-checkbox.remember {
    margin: 0px 0px 35px 0px;
  }

  .smsStyle{
    display: inline-block;
    height: 38px;
    width: 28%;
    vertical-align: bottom;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    text-align: center;
    cursor: pointer;
  }
</style>
